import React from 'react'
import { Modal, Tabs, message } from 'antd'
import axios from 'axios'
import { checkres } from 'utils'
import Table from './Table'

export default function ModalTable(props) {
  const { visible, onCancel, forwardCfgId } = props
  const [loading, setLoading] = React.useState()
  const [num, setNum] = React.useState('1')
  const [rowKeys, setRowKeys] = React.useState([])
  const [rowKeys2, setRowKeys2] = React.useState([])

  function onSubmit() {
    let str = null
    let vehicleIds = null
    if (num === '1') {
      str = 'stopforwardvehicle'
      vehicleIds = rowKeys.join(',')
    } else if (num === '2') {
      str = 'forwardvehicle'
      vehicleIds = rowKeys2.join(',')
    }
    if (!str) return
    if (!vehicleIds) {
      message.warning('请选择数据')
      return
    }
    setLoading(true)
    axios
      .get('/forwardcfgvehicle/' + str, {
        params: {
          forwardCfgId: forwardCfgId,
          vehicleIds: vehicleIds,
        },
      })
      .then(res => {
        setLoading(false)
        if (!checkres(res)) return
        message.success(res.data.msg)
        onCancel()
        setRowKeys([])
        setRowKeys2([])
      })
  }

  function callbackTab(key) {
    setNum(key)
  }

  return (
    <Modal
      visible={visible}
      title="转发车辆管理"
      onCancel={onCancel}
      onOk={onSubmit}
      maskClosable={false}
      okText={num === '1' ? '取消转发' : '转发'}
      width={900}
      confirmLoading={loading}
    >
      <Tabs defaultActiveKey="1" onChange={callbackTab}>
        <Tabs.TabPane tab="已转发" key="1">
          <Table
            forwardCfgId={forwardCfgId}
            setRowKeys={v => setRowKeys(v)}
            rowKeys={rowKeys}
            num={num}
            type="1"
            url="forwardcfgvehiclelist"
          />
        </Tabs.TabPane>
        <Tabs.TabPane tab="未转发" key="2">
          <Table
            forwardCfgId={forwardCfgId}
            setRowKeys={v => setRowKeys2(v)}
            rowKeys={rowKeys2}
            num={num}
            type="2"
            url="notforwardcfgvehiclelist"
          />
        </Tabs.TabPane>
      </Tabs>
    </Modal>
  )
}
